<%--
  Created by IntelliJ IDEA.
  User: gaosubo
  Date: 2020/9/12
  Time: 18:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE HTML>
<html>
<head>
    <title>申报任务管理</title>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="stylesheet" href="/lib/layui/layui/css/layui.css">
    <link rel="stylesheet" href="/lib/zTree_v3/css/zTreeStyle/zTreeStyle.css">
    <link rel="stylesheet" href="/lib/layui/layui/css/style.css?20200325.1">
    <link rel="stylesheet" href="/lib/layui/layui/css/common.css">

    <script type="text/javascript" src="/js/jquery/jquery-1.9.1.js"></script>
    <script src="/lib/jquery.form.min.js"></script>
    <script src="/lib/layui/layui/layui.js"></script>
    <script src="/lib/layui/layui/js/common.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/layui/layui/js/baseCode/base.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="/lib/layui/layui/global.js?20191126"></script>
    <script src="/js/base/base.js" type="text/javascript" charset="utf-8"></script>
    <script src="/js/jquery/jquery.cookie.js"></script>
</head>
<style>
    .btn {
        text-align: right;
    }
    .layui-form-item {
        margin-bottom: 5px;
    }
    .layui-input-inline{
        width: 300px !important;
    }
    .layui-icon-ok{
        margin-top: 6px;
    }
    .openFile input[type=file]{
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 18px;
        z-index: 99;
        opacity: 0;
    }
    .information {
        background: #e8f4fc;
        height: 30px;
        line-height: 30px;
    }
</style>
<body>
    <div>
        <div class="item">
            <img src="../img/chengguo.png" alt="" style="margin: 0 5px 0 20px;"> <span
                style="font-size: 22px;display: inline-block;vertical-align: middle;">申报任务管理</span>
        </div>
        <hr class="layui-bg-blue">
        <div>
            <div class="btn">
                <div style="margin-left: 10px;float: left" class="layui-form">
                    <input type="radio" name="status" value="" lay-filter="status" title="全部" checked>
                    <input type="radio" name="status" value="1"  lay-filter="status" title="申报中">
                    <input type="radio" name="status" value="2"  lay-filter="status" title="结束">
                </div>
                <div>
                    <button type="button" class="layui-btn add layui-btn-sm " id="newAdd" style="" >
                        <i class="layui-icon layui-icon-addition"></i>
                        新建
                    </button>
                    <button type="button" class="layui-btn immp layui-btn-sm "onclick="clicked1();" id="del" style="margin-right: 15px" >
                        <i class="layui-icon">&#xe601;</i>
                        导出
                    </button>
                </div>
            </div>
        </div>
        <div>
            <table class="layui-hide" id="test" lay-filter="test"></table>
        </div>
    </div>

</body>
</html>
<script type="text/html" id="barDemo">
    <div class="layui-btn-container">
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">编辑</a>
<%--
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
--%>
    </div>
</script>
<script>
    var table,layer,form,upload,laydate,planId;
    $(function () {
        layui.use(['table', 'layer', 'form','laydate','upload'], function () {
            var table = layui.table
                , layer = layui.layer
                , form = layui.form
                , upload = layui.upload
                , laydate = layui.laydate

            //渲染表格
            var taskmanager=table.render({
                elem: '#test'
                ,url:'/rpmTopic/getRecogize'
                ,where:{
                    ctreateType:3
                },
                page:true
                , cols: [[
                    {field: 'planName', title: '任务名称', align: 'center'}
                    , {field: 'state', title: '申报状态', align: 'center',templet:function (d) {
                            if(d.state==1){
                                return '申报中'
                            }else if(d.state==2){
                                return '已结束'
                            }
                        }}
                    , {field: 'num', title: '申报数量', align: 'center'} //minWidth：局部定义当前单元格的最小宽度，layui 2.2.1 新增
                    , {field: 'bynum', title: '通过数量', align: 'center'}
                    , {field: 'beginDate', title: '开始日期', align: 'center'}
                    , {field: 'endDate', title: '结束日期',templet: function(d){
                            //当前
                            var timestamp = Date.parse(new Date());
                            var strtime = d.endDate;
                            var date = new Date(strtime.replace(/-/g, '/'));
                            //结束
                            time3 = Date.parse(date);
                            var planid = d.planId +','
                            console.log(planid)
                            if(timestamp - time3 >0){
                                $.ajax({
                                    url: '/rpmTopic/updateRpmPlan',
                                    data: {planId: planid},
                                    dataType: 'json',
                                    type: 'get',
                                })
                            }
                            return d.endDate
                        }
                    }
                    , {field: 'type', title: '操作', toolbar: '#barDemo'}
                ]],
                parseData: function (res) {
                    return {
                        code: 0,
                        msg: res.msg,
                        count: res.totleNum,
                        data: res.obj
                    }
                }
            });
//新建的点击事件
            //新建
            $('#newAdd').click(function () {
                creat(0)
            });
            /*新建和编辑的方法*/
            function creat(type, planId) {
                if (type == '0') {
                    var title = '新建';
                } else {
                    var title = '编辑';
                }
                layer.open({
                    type: 1,
                    resize: true,
                    offset: 'auto',
                    area: ['70%', '90%'],
                    title: title,
                    btn: ['确定', '关闭'],
                    btnAlign: 'c',
                    content: '<div>' +
                    '<form class="layui-form" id="ajaxforms" lay-filter="ajaxforms" style="margin-top: 10px">' +
                    ' <div class="layui-form-item">\n' +
                    '    <div class="layui-inline">\n' +
                    '      <label class="layui-form-label">申报计划</label>\n' +
                    '      <div class="layui-input-inline" style="width: 150px">\n' +
                    '        <input type="text" id="planName" name="planName" class="layui-input">\n' +
                    '      </div>\n' +
                    '    </div>\n' +
                    '  </div>' +

                    ' <div class="layui-form-item">\n' +
                    '    <div class="layui-inline">\n' +
                    '      <label class="layui-form-label">计划描述</label>\n' +
                    '      <div class="layui-input-inline" style="width: 150px">\n' +
                    '        <textarea placeholder="请输入内容" class="layui-textarea" id="des" name="des"></textarea>\n' +
                    '      </div>\n' +
                    '    </div>\n' +
                    '  </div>' +

                    ' <div class="layui-form-item">\n' +
                    '    <div class="layui-inline">\n' +
                    '      <label class="layui-form-label">开始时间</label>\n' +
                    '      <div class="layui-input-inline" style="width: 150px">\n' +
                    '        <input type="text" id="beginDate" name="beginDate" autocomplete="off" class="layui-input">\n' +
                    '      </div>\n' +
                    '    </div>\n' +
                    '  </div>' +

                    ' <div class="layui-form-item">\n' +
                    '    <div class="layui-inline">\n' +
                    '      <label class="layui-form-label">结束时间</label>\n' +
                    '      <div class="layui-input-inline" style="width: 150px">\n' +
                    '        <input type="text" id="endDate" name="endDate" autocomplete="off" class="layui-input">\n' +
                    '      </div>\n' +
                    '    </div>\n' +
                    '  </div>' +

                    ' <div class="layui-form-item">\n' +
                    '    <div class="layui-inline">\n' +
                    '      <label class="layui-form-label">申报状态</label>\n' +
                    '      <div class="layui-input-inline" style="width: 150px">\n' +
                    '        <input type="radio" name="state" value="1" title="申报中" checked>\n' +
                    '        <input type="radio" name="state" value="2" title="申报结束" >\n' +
                    '      </div>\n' +
                    '    </div>\n' +
                    '  </div>' +

                    ' <div class="layui-form-item">\n' +
                    '    <div class="layui-inline">\n' +
                    '      <label class="layui-form-label">上传附件</label>\n' +
                    '      <div class="layui-input-inline" style="width: 150px">\n' +
                    '        <ul id="fileul"></ul>' +
                    '        <button type="button" class="layui-btn layui-btn-sm" id="test1">\n' +
                    '          <i class="layui-icon">&#xe67c;</i>上传附件\n' +
                    '       </button>\n' +
                    '      </div>\n' +
                    '    </div>\n' +
                    '  </div>' +

                    '</form>' +
                    '</div>',
                    success: function (layero, index) {

                        laydate.render({
                            elem: '#beginDate'
                            // ,type: 'datetime'
                            ,trigger: 'click'
                        });
                        laydate.render({
                            elem: '#endDate'
                            // ,type: 'datetime'
                            ,trigger: 'click'
                        });
                        form.render();
                        upload.render({
                            elem: '#test1'
                            ,url: '/upload'
                            ,data:{
                                module:'fixAssets'
                            }
                            ,done: function(res, index, upload){
                                if (res.flag){
                                    var obj = res.obj;
                                    for (var i = 0; i < obj.length; i++) {
                                        var str = '<li>\n' +
                                            '                    <a href="/download?' + obj[i].attUrl + '" name="' + obj[i].attachName + '" attachId="' + obj[i].attachId + '" style="text-decoration:none;margin-left:5px;">\n' +
                                            '                        <img src="/img/attachment_icon.png">\n' +
                                            obj[i].attachName +
                                            '                    </a>\n' +
                                            '                    <img class="deImgs" style="margin-left:5px;cursor: pointer;" src="/img/file/icon_deletecha_03.png">\n' +
                                            '                </li>';
                                        $('#fileul').append(str);
                                    }
                                }else{
                                    layer.msg('上传失败');
                                }
                            }
                            ,accept: 'file'
                        })

                        form.render(); //更新全部
                        if(type==1){
                            $.ajax({
                                url:'/rpmPlan/selectByKey',
                                dataType: 'json',
                                type: 'get',
                                data:{
                                    planId:planId
                                },
                                success:function (res) {
                                    console.log(res)
                                    var str = ''
                                    form.render()
                                    if(res.object.attachmentList.length>0){
                                        for(var i=0;i<res.object.attachmentList.length;i++){
                                            str+='<div class="dech" deUrl="' +res.object.attachmentList[i].attUrl + '"><a href="/download?' + res.object.attachmentList[i].attUrl + '" NAME="' + res.object.attachmentList[i].attachName +'*"><img style="margin-right:10px;" src="/img/attachment_icon.png"/>' + res.object.attachmentList[i].attachName + '</a><img class="deImgs" style="margin-left:5px;cursor: pointer;" src="/img/file/icon_deletecha_03.png"/><input type="hidden" class="inHidden" value="' + res.object.attachmentList[i].aid + '@' + res.object.attachmentList[i].ym + '_' + res.object.attachmentList[i].attachId +',"></div>'
                                        }
                                    }else{
                                        str='';
                                    }
                                    $('#fileul').html(str)
                                    form.val("ajaxforms", res.object);
                                }

                            })
                        }
                    },
                    yes: function (index, layero) {
                        var data1 = form.val("ajaxforms");
                        data1['attachmentId'] = '';
                        data1['attachmentName'] = '';
                        data1['ctreateType'] = '3';
                        //处理附件  attachmentId    attachmentName
                        $("#fileul li").each(function(index){
                            var attachmentName = $(this).find('a').attr('name');
                            var attachId = $(this).find('a').attr('attachId');
                            data1.attachmentId += attachId + ',';
                            data1.attachmentName +=  attachmentName + '*';
                        });
                        // $.ajax({
                        //     url: url,
                        //     dataType: 'json',
                        //     type: 'get',
                        //     data: obj,
                        //     success: function (res) {
                        //         if (res.flag) {
                        //             layer.msg('新增成功！', {icon: 1});
                        //             tableInt.reload()
                        //             layer.closeAll();
                        //         }
                        //     }
                        // })

                        var starttime = $('#beginDate').val();
                        var endtime = $('#endDate').val();
                        var start = new Date(starttime.replace("-", "/").replace("-", "/"));
                        var end = new Date(endtime.replace("-", "/").replace("-", "/"));
                        if(end<start){
                            layer.msg('结束日期不能小于开始日期！', {icon: 1});
                            return false;
                        }
                        if(type == 0){
                            var url = '/Recognize/insertRecogize'
                        }else if(type == 1){
                            var url = '/Recognize/updateRecogizePlan'
                            data1.planId = planId
                        }

                        $.ajax({
                            url: url,
                            dataType: 'json',
                            type: 'get',
                            data: data1,
                            success: function (res) {
                                if (res.flag) {
                                    layer.msg('保存成功！', {icon: 1});
                                    taskmanager.reload()
                                    layer.closeAll();
                                }
                            }
                        })
                    }
                });

            }
            $('#newAdd').click(function () {
            });
            //监听顶部单选按钮
            form.on('radio(status)', function(data){
                var value = data.value;

                table.reload('test', {
                    where:{
                        state:value
                    }
                });

            });

            //监听表格工具事件
            table.on('tool(test)', function(obj){
                var data = obj.data;
                var layEvent = obj.event;
                var tr = obj.tr;
                planId=data.planId;
                if(layEvent === 'edit'){
                    creat(1,planId)
                }else if(layEvent ==='del'){
                    layer.confirm('真的删除当前数据吗？', function (index) {
                        $.ajax({
                            type: 'get',
                            url: '/rpmPlan/deleteByKey?planId='+planId,
                            dataType: 'json',
                            success: function (res) {
                                if (res.msg == 'ok') {
                                    layer.msg('删除成功！', {icon: 1});
                                    taskmanager.reload()
                                    layer.closeAll();
                                }
                            }
                        })
                    });
                }
            });
//删除附件
            $(document).on('click', '.deImgs', function () {
                var _this = this;
                var attUrl = $(this).parents('.dech').attr('deUrl');
                layer.confirm('确定删除该附件吗？', function (index) {
                    $.ajax({
                        type: 'get',
                        url: '/delete?' + attUrl,
                        dataType: 'json',
                        success: function (res) {

                            if (res.flag == true) {
                                layer.msg('删除成功', {icon: 6, time: 1000});
                                $(_this).parent().remove();
                            } else {
                                layer.msg('删除失败', {icon: 2, time: 1000});
                            }
                        }
                    })
                });
            });
        })

    })

    //   导出Excel
    function clicked1() {
        window.location.href = "/rpmTopic/selectionExport"
    }

</script>
